package co.bugu.code.analysis.service;

import co.bugu.code.analysis.service.entity.UserCodeCount;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author daocers
 * @date 2025/3/29 16:06
 */
@Service
public class FileService {

    /**
     * 获取给定目录下的所有git目录
     * <p>
     * 目录下有.git文件的即可认为是git目录
     *
     * @param targetFile 给定的目录，和git工程的路径关系不确定
     * @return
     * @author alei
     * @date 2025/3/29 16:07
     */
    public List<File> getGitDirectory(File targetFile) {
        List<File> list = new ArrayList<>();
        if (!targetFile.exists()) {
            throw new RuntimeException(String.format("file: %s 不存在", targetFile.getAbsoluteFile()));
        }

        if(targetFile.isDirectory()){
            Boolean isGitDir = isGitDir(targetFile);
            if(isGitDir){
                list.add(targetFile);
            }else{
                File[] files = targetFile.listFiles();
                if(ArrayUtils.isNotEmpty(files)){
                    for(File file: files){
                        List<File> gitDirectory = getGitDirectory(file);
                        if(CollectionUtils.isNotEmpty(gitDirectory)){
                            list.addAll(gitDirectory);
                        }
                    }
                }
            }
        }
        return list;
    }


    private Boolean isGitDir(File file) {
        if (!file.exists() || file.isFile()) {
            return false;
        }
        File[] files = file.listFiles();
        List<File> collect = Arrays.stream(files).filter(item -> item.getName().equals(".git")).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {
            return true;
        }
        return false;
    }


    public List<UserCodeCount> transFileToCodeCount(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));

        List<UserCodeCount> list = new ArrayList<>();
        String line = null;

        String gitPath = null;
        UserCodeCount userCodeCount = new UserCodeCount();
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.startsWith("****")){
                gitPath = line.split("\\s+")[1];

            }else{
                userCodeCount = new UserCodeCount();
                userCodeCount.setGitPath(gitPath);
                String[] lineInfo = line.split("\\s+");
                userCodeCount.setUsername(lineInfo[0]);
                if(lineInfo.length > 2){
                    userCodeCount.setAddCount(Integer.valueOf(lineInfo[2]));
                }
                if(lineInfo.length > 3){
                    userCodeCount.setDeleteCount(Integer.valueOf(lineInfo[3]));
                }
                list.add(userCodeCount);
            }


        }
        return list;
    }
}
